﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DAL.Entities;

namespace DAL.EntitesDAL
{
    public class HocvienDAL: BaseDAL,UTL.IBaseDAL
    {
        #region Implement

        public int Count()
        {
            throw new NotImplementedException();
        }

        public DataTable Select()
        {
            return ExecuteQuery("sp_SelectAll_Hocvien");
        }
    /*    public DataTable Select_Nhapdiem(string id)
        {
            string sql = @"Select *, (Case when Gioitinh=0 then 'Nam' else N'Nữ' end) as Sex 
                           From tb_Hocvien a join tb_Lophoc b on a.MalopID=b.Malop
                                             join tb_Bangdiem c on a.MaHV = c.MaHVID
                           Where b.Malop = '{0}' ";
            sql = string.Format(sql, id);
            return ExecuteQuery(sql);
        }*/
        public DataTable Select_Nhapdiem(string _id)
        {
            string sql = @"Select a.MaHV,a.Hoten
                           From tb_Hocvien a
                           Where a.MalopID = '{0}'";
            sql = string.Format(sql, _id);
            return ExecuteQuery(sql);
        }

        public DataTable Select(object obj)
        {
            throw new NotImplementedException();
        }

        public object GetByKey(object key)
        {
            throw new NotImplementedException();
        }

        public bool Delete(string id)
        {
            var sql = "sp_Delete_Hocvien {0}";
            sql = string.Format(sql, id);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Insert(object obj)
        {
            var o = (Hocvien)obj;
            var sql = "set dateformat dmy; execute sp_Insert_Hocvien '{0}', N'{1}', '{2}', '{3}', N'{4}', '{5}', '{6}','{7}' ";
            sql = string.Format(sql, o.MaHV, o.Hoten, o.Gioitinh, o.Namsinh.ToString("dd/MM/yyyy"), o.Diachi, o.Dienthoai, o.MalopID, o.Done);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Update(object obj)
        {
            var o = (Hocvien)obj;
            var sql = "set dateformat dmy; execute sp_Update_Hocvien '{0}', N'{1}', '{2}', '{3}', N'{4}', '{5}', '{6}'  ";
            sql = string.Format(sql, o.MaHV, o.Hoten, o.Gioitinh, o.Namsinh.ToString("dd/MM/yyyy"), o.Diachi, o.Dienthoai, o.MalopID);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }
        public bool Update(string _id, int done = 1)
        {
            var sql = "set dateformat dmy; execute sp_Update_Hocvien_Done '{0}', '{1}' ";
            sql = string.Format(sql, _id, done);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }
        public DataTable Search(string name)
        {
            var sql = @"Select *, (Case when Gioitinh=0 then 'Nam' else N'Nữ' end) as Sex from tb_Hocvien a join tb_Lophoc b on a.MalopID=b.Malop where MalopID like N'%{0}%' ";
            sql = string.Format(sql, name);
            return ExecuteQuery(sql);
        }

        public DataTable Search(DAL.Entities.Hocvien o)
        {
            string sql;
            if (o.MaHV != "")
                sql = @"Select *, (Case when Gioitinh=0 then 'Nam' else N'Nữ' end) as Sex from tb_Hocvien a join tb_Lophoc b on a.MalopID=b.Malop Where MaHV like '%"+o.MaHV+"%' and Hoten like N'%"+o.Hoten+"%' and Diachi like N'%"+o.Diachi+"%' and Dienthoai like '%"+o.Dienthoai+"%'  ";
            else
                sql = @"Select *, (Case when Gioitinh=0 then 'Nam' else N'Nữ' end) as Sex from tb_Hocvien a join tb_Lophoc b on a.MalopID=b.Malop Where Hoten like N'%" + o.Hoten + "%' and Diachi like N'%" + o.Diachi + "%' and Dienthoai like '%" + o.Dienthoai + "%'  ";
            return ExecuteQuery(sql);
        }


        #endregion

        #region Other

        public DataTable Select_Max()
        {
            var sql = @"Select Max(MaHV) as Ma From tb_Hocvien";
            return ExecuteQuery(sql);
        }

        public DataTable Select_HP(string ma)
        {
            string sql = @"Select Hocphi From tb_Lophoc Where Malop = '{0}' ";
            sql = string.Format(sql, ma);
            return ExecuteQuery(sql);
        }
        public DataTable Select_TK_HV(string ma)
        {
            string sql = @"Select c.MaHV,c.Hoten,(Case when Gioitinh=0 then 'Nam' else N'Nữ' end) as Sex,c.Namsinh,c.Dienthoai,c.Diachi,a.Tenlop
                           From tb_Lophoc a join tb_Khoahoc b on a.MakhoaID=b.Makhoa
				                             join tb_Hocvien c on a.Malop=c.MalopID
                           Where b.Makhoa='{0}' ";
            sql = string.Format(sql, ma);
            return ExecuteQuery(sql);
        }
        public DataTable Select(string makhoa)
        {
            string sql = @" Select b.MaHV,b.Hoten,b.Gioitinh,b.Namsinh,b.Dienthoai,b.Diachi,b.MalopID,a.Tenlop,c.Tenkhoa
                            From tb_Lophoc a join tb_Hocvien b on a.Malop=b.MalopID
				                             join tb_Khoahoc c on a.MakhoaID=c.Makhoa
                            Where c.Makhoa = '{0}' ";
            sql = string.Format(sql, makhoa);
            return ExecuteQuery(sql);
        }
        public DataTable Select_DSDangky(string _malop)
        {
            string sql = @" Select a.MaHV, a.Hoten,a.Namsinh,(Case when Gioitinh=0 then 'Nam' else N'Nữ' end) as Sex,a.Dienthoai,a.Diachi
                            From tb_Hocvien a
                            Where a.MalopID = '{0}'  ";
            sql = string.Format(sql, _malop);
            return ExecuteQuery(sql);
        }
        #endregion
    }
}
